package org.lalib.mediator;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;
import org.lalib.acquire.IMediator;
import org.lalib.entity.object.ECategory;
import org.lalib.foundation.FDataAccess;
import org.springframework.stereotype.Repository;

@Repository(value = "mCategory")
public class MCategory extends FDataAccess<ECategory> implements IMediator {

	private static final long serialVersionUID = 1L;

	public ECategory getRootCategory() {
		return (ECategory) getSession().createCriteria(ECategory.class).add(Restrictions.isNull("parentCategory")).uniqueResult();
	}

	@Override
	protected Class<ECategory> getClassType() {
		return ECategory.class;
	}

	public ECategory getCategoryWithChild(Long id) {
		return (ECategory) getSession().createCriteria(ECategory.class).add(Restrictions.eq("id", id))
				.setFetchMode("childCategories", FetchMode.JOIN).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<ECategory> getAllCategoriesExceptForRootCategory() {
		return (List<ECategory>) getSession().createCriteria(ECategory.class).add(Restrictions.isNotNull("parentCategory")).list();
	}

	@SuppressWarnings("unchecked")
	public List<ECategory> getCategoriesByType(String type) {
		return (List<ECategory>) getSession().createCriteria(ECategory.class).add(Restrictions.eq("type", type)).list();
	}
}